package com.manster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manster.entity.SysMenu;
import com.manster.entity.SysUser;
import com.manster.mapper.SysMenuMapper;
import com.manster.mapper.SysUserMapper;
import com.manster.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manster.service.SysUserService;
import com.manster.util.R;
import com.manster.vo.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author manster
 * @since 2022-03-13
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService userService;

    @Autowired
    SysUserMapper userMapper;

    @Override
    public List<SysMenuVo> getCurrentUserNav() {
        //从security中获取用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser user = userService.getByUsername(username);
        List<Long> menuIds = userMapper.getNavMenuIds(user.getId());
        List<SysMenu> menus = this.listByIds(menuIds);

        //转树状结构
        List<SysMenu> menuTree = buildTreeMenu(menus);

        return convert(menuTree);
    }

    @Override
    public List<SysMenu> treeMenu() {
        //获取所有菜单信息
        List<SysMenu> menus = list(new QueryWrapper<SysMenu>().orderByAsc("order_num"));
        //树状结构
        return buildTreeMenu(menus);
    }


    private List<SysMenuVo> convert(List<SysMenu> menuTree) {
        List<SysMenuVo> sysMenuVoList = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setId(m.getId());
            sysMenuVo.setName(m.getPerms());
            sysMenuVo.setTitle(m.getName());
            sysMenuVo.setPath(m.getPath());
            sysMenuVo.setComponent(m.getComponent());
            sysMenuVo.setIcon(m.getIcon());

            if(m.getChildren().size()>0){
                sysMenuVo.setChildren(convert(m.getChildren()));
            }

            sysMenuVoList.add(sysMenuVo);
        });

        return sysMenuVoList;
    }

    //转换菜单为树状结构
    //遍历
    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> res = new ArrayList<>();

        for (SysMenu menu : menus) {
            for (SysMenu m : menus) {
                if(menu.getId().equals(m.getParentId())){
                    menu.getChildren().add(m);
                }
            }

            if(menu.getParentId()==0L){
                res.add(menu);
            }
        }

        return res;
    }

    //递归
    private List<SysMenu> recBuildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> rootMenus = menus.stream()
                .filter(m -> m.getParentId() == 0)
                .peek(root -> root.setChildren(getChildrens(root,menus)))
                .collect(Collectors.toList());

        return rootMenus;
    }

    //查找子菜单
    private List<SysMenu> getChildrens(SysMenu root, List<SysMenu> menus) {

        List<SysMenu> childrens = menus.stream()
                .filter(m -> root.getId().equals(m.getParentId()))
                .peek(parent -> parent.setChildren(getChildrens(parent, menus)))
                .collect(Collectors.toList());

        return childrens;
    }
}
